<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-row type="flex" justify="space-between" align="middle">
          <el-row>
            <!-- 筛选 -->
            <el-form inline :model="formInline" class="demo-form-inline">
              <el-form-item label="关键字" placeholder="根据文章标题搜索">
                <el-input v-model="formInline.keyword"></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="formInline.state" placeholder="请选择">
                  <el-option label="启用" value="1"></el-option>
                  <el-option label="禁用" value="0"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button @click="removeList">清除</el-button>
                <el-button type="primary" @click="searchList">搜索</el-button>
              </el-form-item>
            </el-form>
          </el-row>
          <!-- 新增 -->
          <el-row type="flex" justify="end" align="middle">
            <el-button type="success" icon="el-icon-edit" @click="showDialog=true"><i></i>新增技巧</el-button>
          </el-row>
        </el-row>
        <!-- 数据记录 -->
        <el-alert type="info" :closable="false" show-icon :title="`数据一共`+this.page.total+'条'"></el-alert>
        <template>
          <!-- 列表数据渲染 -->
          <el-table :data="list">
            <el-table-column fixed prop="date" label="序号"  type="index" width="100">
            </el-table-column>
            <el-table-column prop="title" label="文章标题" width="400">
              <template v-slot="{row}">
                {{row.title}}
                <i @click="video(row.videoURL)" class="vid el-icon-film" v-if="row.videoURL"></i>
              </template>
            </el-table-column>
            <el-table-column prop="visits" label="阅读数" width="150">
            </el-table-column>
            <el-table-column prop="username" label="录入人" width="150">
            </el-table-column>
            <el-table-column prop="createTime" label="录入时间" width="300">
              <template v-slot="{row}">
                {{row.createTime|formatDate("yyyy-MM-dd hh:mm:ss")}}
              </template>
            </el-table-column>
            <el-table-column prop="state" label="状态" width="100">
              <template v-slot="{row}">
                <span>{{ row.state===1?'已启用':'已禁用'}}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="200">
                <template v-slot="{row}">
                  <el-button type="text" @click="articles(row.id)">预览</el-button>
                  <el-button type="text" @click="stateshow({id:row.id,state:(row.state===1? 0:1)})">{{row.state===1?'禁用':'启用'}}</el-button>
                  <el-button type="text" :disabled="!row.state" @click="editSkill(row.id)" >修改</el-button>
                  <el-button type="text" :disabled="!row.state" @click="delList(row.id)">删除</el-button>
                </template>
            </el-table-column>
          </el-table>
        </template>
        <!-- 放置分页组件 -->
        <el-row style="height: 80px" type="flex" justify="end" align="middle">
          <el-pagination
          @current-change="changePage"
            :total="page.total"
            :current-page="page.page"
            :page-size="page.size"
            layout="prev,pager,next,sizes,jumper"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 新增和修改组件 -->
    <articles-add ref="addDept" :show-dialog.sync="showDialog" @addDepts="getlist" />
    <!-- 预览组件 -->
    <articles-preview ref="preview" :ashow-dialog.sync="ashowDialog" />
    <!-- 视频播放 -->
    <div class="video" v-if="videoUrl">
      <div class="video-ico" @click="closeVideo()"><span class="el-icon-close"></span></div>
      <div class="video-box">
        <video autoplay controls="controls" >
          <source :src="videoUrl" type="video/mp4">
        </video>
      </div>
    </div>
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/articles.js'
import ArticlesAdd from '../components/articles-add.vue'
import ArticlesPreview from '../components/articles-preview.vue'
export default {
  // 组件引入
  components: {
    ArticlesAdd,
    ArticlesPreview
  },
  data () {
    return {
      // 筛选表单数据
      formInline: {
        keyword: '',
        state: ''
      },
      //  定义分页数据
      page: {
        page: 1, // 默认第一页
        size: 10,
        total: 0
      },
      // 列表数据
      list: [],
      // 视频路径
      videoUrl: null,
      showDialog: false, // 控制新增和修改弹窗
      ashowDialog: false // 控制预览弹窗
    }
  },
  created () {
    this.getlist()
  },
  methods: {
    // 获取列表数据
    async getlist () {
      const res = await list(this.page)
      this.page.total = res.data.counts
      this.list = res.data.items
    },
    // 切换页码
    changePage (newPage) {
      this.page.page = newPage
      this.getlist()
    },
    // 删除列表数据
    async delList (id) {
      const data = { id }
      try {
        await this.$confirm('您确定要删除该文章吗?')
        await remove(data)
        this.getlist()
        this.$message.success('删除文章成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 筛选列表数据
    async searchList () {
      const res = await list(this.formInline)
      this.page.total = res.data.counts
      this.list = res.data.items
    },
    // 清除表单
    removeList () {
      this.formInline = {
        keyword: '',
        state: ''
      }
      this.getlist()
    },
    // 修改功能
    async editSkill (data) {
      this.showDialog = true
      // 调用子组件方法
      this.$refs.addDept.update(data)
    },
    // 预览功能
    async articles (id) {
      this.ashowDialog = true
      // 调用子组件方法
      this.$refs.preview.preview(id)
    },
    // 修改状态
    async stateshow (data) {
      await changeState(data)
      this.getlist()
    },
    // 点击关闭视频预览
    closeVideo () {
      this.videoUrl = null
    },
    // 视频预览
    video (URL) {
      this.videoUrl = URL
      console.log(this.videoUrl)
    }
  }
}
</script>
<style scoped>
.demo-form-inline{
  padding-top: 25px;
}
.totle {
  height: 40px;
  line-height: 40px;
  background-color: #f4f4f4;
}
.btnstyle{
  margin-right: 10px;
  color: blue;
}
.vid{
  font-size: 20px;
  color: blue;
}
.video{
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  overflow: hidden;
  z-index: 9999;
}
.video-ico {
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%, 0);
    background: rgba(0, 0, 0, 0.4);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
}
 .video-box {
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}
video {
      width: 100%;
      height: 100%;
    }
</style>
